//
//  DGTabBarViewController.m
//  TabBarController
//
//  Created by yaoqi yaoqi on 2017/7/13.
//  Copyright © 2017年 duandonggang. All rights reserved.
//

#import "DGTabBarViewController.h"
#import "HomeViewController.h"
#import "DGNavigationViewController.h"
//控制器
#import "HomeViewController.h"
#import "MallViewController.h"
#import "VRController.h"
#import "ShopingCarViewController.h"
#import "PersonalCenterViewController.h"
//其他类
#import "FilesHeader.h"
#import "CDTabBar.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
@interface DGTabBarViewController ()
@property(nonatomic,strong)UIView *topBarV;
@property (nonatomic,assign) NSInteger  indexFlag;//记录上一次点击tabbar，使用时，记得先在init或viewDidLoad里 初始化 = 0

@end

@implementation DGTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _indexFlag = 0;
    //定义tabBar
    //实例化自控制器
    [self setChildVC];
    [self InitMiddleView];
    self.tabBar.tintColor = [UIColor blackColor];
//    //设置tabbar样式
    self.tabBar.barStyle = UIBarStyleBlack;
    //设置tabbar的背景颜色
    [UITabBar appearance].translucent = NO;
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    
//
//    //添加阴影
//    self.tabBar.layer.shadowColor = rgb(204, 204, 204).CGColor;
//    self.tabBar.layer.shadowOffset = CGSizeMake(0, -2);
//    self.tabBar.layer.shadowOpacity = 1;
//    self.tabBar.layer.shadowRadius = 6;
//
//    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0,-5)];
}
- (void)InitMiddleView
{
    CDTabBar *tabBar = [[CDTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
    [tabBar setDidMiddBtn:^{
        VRController *vc = [[VRController alloc] init];
        DGNavigationViewController *nav = [[DGNavigationViewController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
    }];
}
-(void)setChildVC{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    [self setChildController:homeVC title:@"首页" normalImage:@"home_icon_1_d" selectedImage:@"home_icon_1_s"];
    
    MallViewController *mallVC = [[MallViewController alloc] init];
    [self setChildController:mallVC title:@"商城" normalImage:@"home_icon_2_d" selectedImage:@"home_icon_2_s"];

    ShopingCarViewController *integralVC = [[ShopingCarViewController alloc] init];
    [self setChildController:integralVC title:@"购物车" normalImage:@"home_icon_4_d" selectedImage:@"home_icon_4_s"];

    PersonalCenterViewController *friendVC = [[PersonalCenterViewController alloc] init];
    [self setChildController:friendVC title:@"我的" normalImage:@"home_icon_5_d" selectedImage:@"home_icon_5_s"];
}

-(void)setChildController:(UIViewController *)controller title:(NSString *)string normalImage:(NSString *)image selectedImage:(NSString *)selectedImage{
    
    //设置tabbar底部的文字
    controller.tabBarItem.title = string;
    //设置文字的大小
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:10];//[UIFont fontWithName:@"HelveticaNeue-Bold" size:10];//[UIFont systemFontOfSize:10];
//    [controller.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    controller.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:TabBarColor} forState:UIControlStateSelected];
    

    DGNavigationViewController *nav = [[DGNavigationViewController alloc]initWithRootViewController:controller];
    
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:10]};
    [[UIBarButtonItem appearance]setTitleTextAttributes:attributes
                                               forState:UIControlStateNormal];
    
    [controller.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [controller.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateSelected];
    
    //加入到tabbar中
    [self addChildViewController:nav];
    
    
//    for (UITabBarItem * i in self.tabBar.items) {
//    }
}
//-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
//    NSInteger index = [self.tabBar.items indexOfObject:item];
//    if (index != self.indexFlag) {
//        //执行动画
//        NSMutableArray *arry = [NSMutableArray array];
//        for (UIView *btn in self.tabBar.subviews) {
//            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//                [arry addObject:btn];
//            }
//        }
//        //添加动画
//        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        //速度控制函数，控制动画运行的节奏
//        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        animation.duration = 0.2;       //执行时间
//        animation.repeatCount = 1;      //执行次数
//        animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
//        animation.fromValue = [NSNumber numberWithFloat:0.1];   //初始伸缩倍数
//        animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
//        [[arry[index] layer] addAnimation:animation forKey:nil];
//        
//        self.indexFlag = index;
//    }
//}

@end
